from flask import Flask
from flask import request,render_template


app = Flask('hiflask')
@app.route('/hi',methods=['GET','POST'])
def hi():
    #使用request请求对象获取请求方法
    if request.method=="GET":
        return """
        <h1>用户登录</h1>
        <form  action="/hi" method="post">
        <input name="name" placeholder="用户名"><br>
        <input name="pwd" placeholder="口令"><br>
        <button>提交</button>
        </form>
        """
    else:
        #获取表单的参数
        name = request.form.get('name')
        pwd = request.form.get('pwd')
        if all((
            name == 'disen',
            pwd == '987'
        )):
            return """
            <h2 style="color:blue;">登陆成功</h2>
            """
        else:
            return """
                <h2 style="color:orange;">登陆失败,<a href = "/hi">重试</a></h2>
             """

@app.route('/bank',methods=['POST','GET'])
def addBank():
    if request.method =='GET':
    #加载数据(Model交互操作)
        data = {
            'title':'绑定银行卡',
            'error_message':'卡号和银行名称不能为空'
        }
        #渲染模版
        html = render_template('band_edit.html',**data)
        print('渲染之后的html',html,sep='\n')
        # raise Exception('有意出现一个异常')
        return html
    else:
        name = request.form.get('name')
        card_num = request.form.get('card_num')
        if  all((
            not name,
            not card_num
        )):
            return """ <h2>登陆成功/h2>"""
        else:
            return """ <h2>银行卡和密码不能为空 </h2>"""

#启动服务
app.run('localhost',
        5000,
        True,
        threaded=True,
        processes = 1)